package com.doxiaopu.students.user.service.impl;

import com.doxiaopu.students.user.entity.MerchantData;
import com.doxiaopu.students.user.entity.User;
import com.doxiaopu.students.user.mapper.MerchantDataMapper;
import com.doxiaopu.students.user.mapper.MerchantMapper;
import com.doxiaopu.students.user.mapper.UserMapper;
import com.doxiaopu.students.user.service.IMerchantService;
import com.doxiaopu.students.user.service.IUserService;
import com.doxiaopu.students.user.vo.MerchantListVO;
import com.doxiaopu.students.user.vo.MerchantVO;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.context.annotation.Primary;
import org.springframework.stereotype.Service;
import org.springframework.transaction.annotation.Transactional;

import java.util.Date;
import java.util.HashMap;
import java.util.List;
import java.util.Map;

@Service
@Primary
public class MerchantServiceImpl implements IMerchantService {
    @Autowired
    private MerchantMapper merchantMapper;
    @Autowired
    private IUserService iUserService;
    @Autowired
    private UserMapper userMapper;
    @Autowired
    private MerchantDataMapper merchantDataMapper;

    @Override
    @Transactional
    public Integer CreateMerchant(MerchantVO merchantVO, Integer id) {
        //创建用户信息
        User createUser = userMapper.getById(id);
        Date now = new Date();
        User user = new User();
        user.setPassword(merchantVO.getPassword());
        user.setUsername(merchantVO.getUsername());
        user.setTypes(1);
        user.setCreatedUser(createUser.getUsername());
        user.setModifiedUser(createUser.getUsername());
        user.setCreatedTime(now);
        user.setModifiedTime(now);
        User usered = iUserService.reg(user);
        //创建商户数据信息
        MerchantData merchantData =new MerchantData();
        merchantData.settUserId(usered.getTUserId());
        merchantData.setCreatedUser(createUser.getUsername());
        merchantData.setCreatedTime(now);
        merchantData.setModifiedUser(createUser.getUsername());
        merchantData.setModifiedTime(now);
        addMerchantData(merchantData);
        //创建商户基本信息
        merchantVO.setCreatedUser(createUser.getUsername());
        merchantVO.setModifiedUser(createUser.getUsername());
        merchantVO.setCreatedTime(now);
        merchantVO.setModifiedTime(now);
        merchantVO.settUserId(usered.getTUserId());
        Integer row = addNewMerchant(merchantVO);
        return row;
    }

    @Override
    public Map<String, Object> findMerchantList(String phone, String storeName, Integer currentPage, Integer pageSize) {
        currentPage = (currentPage - 1) * pageSize;
        Map<String, Object> res = new HashMap<>();
        List<MerchantListVO> data = findMerchants(phone,storeName,currentPage,pageSize);
        Integer total = findTotal();
        res.put("data",data);
        res.put("total",total);
        return res;
    }

    @Override
    @Transactional
    public Integer updateMerchant(MerchantVO merchantVO,Integer id) {
        User createUser = userMapper.getById(id);
        Date now = new Date();
        User user = new User();
        user.setTUserId(merchantVO.gettUserId());
        user.setPassword(merchantVO.getPassword());
        user.setModifiedUser(createUser.getModifiedUser());
        user.setModifiedTime(now);
        updatePwdInfo(user);
        merchantVO.setModifiedUser(createUser.getModifiedUser());
        merchantVO.setModifiedTime(now);
        Integer row = updateMerchantInfo(merchantVO);
        return row;
    }

    /**
     * 增加商户的基础信息
     * @param merchantVO
     * @return
     */
    private Integer addNewMerchant(MerchantVO merchantVO){
        return merchantMapper.addNewMerchant(merchantVO);
    };

    /**
     * 查询商家列表
     * @param phone
     * @param storeName
     * @param currentPage
     * @param pageSize
     * @return
     */

    private List<MerchantListVO> findMerchants(String phone, String storeName, Integer currentPage, Integer pageSize){
        return merchantMapper.findMerchants( phone,  storeName,  currentPage,  pageSize);
    };
    /**
     * 查询商家总量
     * @return
     */
    private Integer findTotal() {
        return merchantMapper.findTotal();
    };
    /**
     * 更改商家店铺信息
     * @param merchantVO
     * @return
     */
    private Integer updateMerchantInfo(MerchantVO merchantVO){
        return merchantMapper.updateMerchantInfo(merchantVO);
    };
    /**
     * 修改商家密码
     * @param user
     * @return
     */
    private Integer updatePwdInfo(User user){
        return userMapper.updatePwdInfo(user);
    };

    /**
     * 增加商户的数据信息
     * @param merchantDataMapper
     * @return
     */
    private Integer addMerchantData(MerchantData merchantData){
        return merchantDataMapper.addMerchantData(merchantData);
    };

}
